#!/usr/bin/python

from random_file import *
import random
import numpy
import sys

'''
sys.argv[1]: dataset size

Generator for:

class WebUser(db.Model):
    name = db.StringProperty()
    city = db.StringProperty()
    state = db.StringProperty()
    sex = db.IntegerProperty() # 0:female, 1:male    

class LogAction(db.Model):
    user = db.ReferenceProperty(WebUser, collection_name='logs')
    action = db.IntegerProperty() # 0:click, 1:read, 2:change
    timespan = db.FloatProperty()
    sourceIp = db.IntegerProperty()
'''

if __name__ == '__main__':
	data_num = sys.argv[1]
	user_dict = dict()
	zipf_list_name = iniZif(data_num, 1)
        zipf_list_timespan = numpy.random.permutation(zipf_list_name)
        zipf_list_city = iniZif(1000, 1)
        city_num = len(zipf_list_city)
        zipf_list_state = iniZif(100, 1)
        state_num = len(zipf_list_state)	
	
	f_user = open('logUser_' + data_num + '.csv', 'a')
        f_user.write('key,name,city,state,sex\n')
 	f_log = open('log_' + data_num + '.csv', 'a')
	f_log.write('key,user,action,sourceIp,timespan\n')
        user_key = 0
	log_key = 0
        for i in range(int(data_num)):
            userId = int(zipf_list_name[i])
            if userId not in user_dict:
	        user_key += 1 
                userName = 'user' + str(userId)
                city = 'city' + zipf_list_city[i%city_num]
                state = 'state' + zipf_list_state[i%state_num]
                sex = random.randint(0, 1)
	        user_dict[userId] = user_key
		f_user.write(str(user_key) + ',' + userName + ',' + city + ',' + state + ',' + str(sex) + '\n');
		
            action = random.randint(0, 2)
            timespan = float(zipf_list_timespan[i])*1.28
            # ipAddr is choosen from the five ips of a given user
            ipaddrs = [(userId<<16)%(2**32), (userId<<17)%(2**32), (userId<<18)%(2**32), (userId<<19)%(2**32), (userId<<20)%(2**32)]
	    log_key += 1
	    user_foreign_key = user_dict[userId]
	    f_log.write(str(log_key) + ',' + str(user_foreign_key) + ',' + str(action) + ',' + 
			str(ipaddrs[random.randint(0,4)]) + ',' + str(timespan) + '\n')
	    
	f_user.close()
	f_log.close()
